﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataStore;
using LogParser.DTO;

namespace AppLogic.Mapper
{
    public class LineDocumentVisitRecord
    {
        public static VisitRecord From(LineDocument lineDoc, Store store, FileRecord file, SiteRecord site)
        {
            VisitRecord visit = store.CreateVisitRecord( file, site );

            visit.Visit = lineDoc.Visit;
            visit.BytesRecieved = TryParseDefault( lineDoc.BytesRecieved );
            visit.BytesSent = TryParseDefault( lineDoc.BytesSent );
            visit.ClientIP.Name = lineDoc.SourceIP;
            visit.ComputerName.Name = lineDoc.ComputerName;
            visit.Cookie.Name = lineDoc.Cookie;
            visit.Host.Name = lineDoc.Host;
            visit.Method.Name = lineDoc.Method;
            visit.Port = TryParseDefault( lineDoc.Port);
            visit.ProtocolVersion.Name = lineDoc.ProtocolVersion;
            visit.Query.Name = lineDoc.Query;
            visit.Referer.Name = lineDoc.Referer;            
            visit.ServerIP.Name = lineDoc.ServerIp;
            visit.Status = TryParseDefault( lineDoc.Status );
            visit.SubStatus = TryParseDefault( lineDoc.SubStatus );
            visit.TimeTaken = TryParseDefault( lineDoc.TimeTaken );
            visit.UserAgent.Name = lineDoc.UserAgent;
            visit.URI.Name= lineDoc.URI;            
            visit.Username.Name = lineDoc.Username;
            visit.Win32Status = TryParseDefault( lineDoc.Win32Status );

            return visit;            
        }

        private static long TryParseDefault(string text, long @default = 0L)
        {
            long l;
            if (!long.TryParse(text, out l))
                l = @default;

            return l;
        }
    }
}
